package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.DynaIndex;
import cn.pconline.autoclub.entity.DynaInfo;
import org.gelivable.dao.GeliDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;

/**
 * 动态索引
 *
 * @author fgs
 *
 */
public class DynaIndexService {

    @Autowired
    private GeliDao geliDao;
    private static final Logger log = LoggerFactory.getLogger(DynaIndexService.class);

    /**
     * 生成动态索引
     *
     * @param dynaInfo
     */
    public long createDynaIndex(DynaInfo dynaInfo) {
        DynaIndex index = new DynaIndex();
        index.setDynaInfoId(dynaInfo.getDynaInfoId());
        index.setStatus(DynaInfo.STATUS_NORMAL);
        index.setCreateDate(dynaInfo.getCreateDate());
        return geliDao.create(index);
    }

    /**
     * 根据动态id查找动态索引
     *
     * @param dynaInfoId
     * @return
     */
    public DynaIndex findDynaIndex(long dynaInfoId) {
        try {
            return geliDao.find(DynaIndex.class, dynaInfoId, dynaInfoId);
        } catch (EmptyResultDataAccessException e) {
            log.warn("can't find dynaIndex which dynaInfoId is {}", dynaInfoId);
            return null;
        }

    }
    
    /**
     * 逻辑删除动态索引（索引删除了，等同于物理删除）
     * @param dynaInfoId
     */
    public void deleteDynaIndex(long dynaInfoId){
    	DynaIndex dynaIndex = findDynaIndex(dynaInfoId);
    	if(dynaIndex!=null){
    		dynaIndex.setStatus( DynaInfo.STATUS_DELETE);
    		geliDao.update( dynaIndex, "status");
    	}
    }
}
